package medios.util;

public class PagingBlockUtil {
	StringBuffer pb;
	int curPage;
	int totPage;
	String funcName;
	String selected_color;
	String normal_color;
	int paging_block_size;
	int paging_size;
	int tot;
	String prev_image;
	String next_image;
	String prevX_image;
	String nextX_image;
	
	public PagingBlockUtil() {
		pb					= new StringBuffer();
		paging_size			= 25;
		paging_block_size	= 10;
		selected_color		= "green";
		normal_color		= "black";
		prev_image   		= "<img src='/images/btn_prev.gif' width='18' height='18' border='0' align='middle' />";
		next_image   		= "<img src='/images/btn_next.gif' width='18' height='18' border='0' align='middle' />";
	}
	public PagingBlockUtil(int curPage , int totPage , String funcName) {
		paging_size			= 10;
		paging_block_size	= 10;
		this.curPage = curPage;
		this.tot = totPage;
		this.funcName = funcName;
		pb					= new StringBuffer();
		
		prev_image   		= "<img src='/img/prev.jpg' width='10' height='11' border='0' align='absmiddle' />";
		next_image   		= "<img src='/img/next.jpg' width='10' height='11' border='0' align='absmiddle' />";
		prevX_image   		= "<img src='/img/pprev.jpg' width='14' height='11' border='0' align='absmiddle' />";
		nextX_image   		= "<img src='/img/nnext.jpg' width='14' height='11' border='0' align='absmiddle' />";
		makePB4();
		
	}
	
	public PagingBlockUtil(int curPage , int tot , String funcName , String selected , String normal , String prev_img , String next_img , String prevX_img , String nextX_img ) {
		
	}
	
	public void setPrev_image(String img_src) {
		prev_image   		= "<img src='"+img_src+"' width='18' height='18' border='0' align='absmiddle' />";
	}
	public void setNext_image(String img_src) {
		next_image   		= "<img src='"+img_src+"' width='18' height='18' border='0' align='absmiddle' />";
	}
	
	public void setPaging_size(int size) {
		paging_size = size;
	}
	public void setColor(String color) {
		selected_color = color;
	}
	public void setNormalColor(String color) {
		normal_color = color;
	}
	public void setCurPage(int cur) {
		curPage = cur;
	}
	public void setTotPage(int tot) {
		this.tot = tot;
 		
	}
	public String getFuncName() {
		return funcName;
	}
	
	public void setFuncName(String fn) {
		funcName = fn;
	}
	
	public void makePB() {
		pb = new StringBuffer();
		int lastPageNum;
		boolean isLast = false;
		boolean isFirst = false;
		
		totPage = (tot / paging_size) + (tot%paging_size==0?0:1);
		if(curPage <= paging_block_size) {
			isFirst = true;
		}
		
		if( (curPage-1) / paging_block_size == (totPage-1) / paging_block_size) {
			lastPageNum = totPage % paging_block_size;
			if(lastPageNum != paging_block_size) {
				isLast =true;
			}
			if(lastPageNum == 0) {
				lastPageNum = paging_block_size;
			}
		}
		else {
			lastPageNum = paging_block_size;
		}
		int start = curPage - (curPage%paging_block_size == 0 ? paging_block_size : curPage%paging_block_size) + 1;
		int end = curPage - (curPage%paging_block_size == 0 ? paging_block_size : curPage%paging_block_size) + lastPageNum;

		System.out.println("totPage : " + totPage);
		System.out.println("lastPageNum : " + lastPageNum);
		System.out.println("curPage : " + curPage);
		System.out.println("start : " + start);
		System.out.println("end : " + end);
		if(start > end) end = start;
		String fn = "";
		String a  = "";
		int i = 0;
		if(!isFirst) {
			fn = funcName + "(" + (start-1) + ")";
			a = "<a href='javascript:" + fn + "'>" + prev_image + "</a> | ";
			pb.append(a);
		}
		for(i = start ; i <= end ; i++) {
			fn = funcName + "(" + i + ")";
			if(i == curPage) {
				a = "<font color='"+selected_color+"'>" + i + "</font>";
			}
			else {
				a = "<a href='javascript:" + fn + "'><font color='"+normal_color+"'>" + i + "</font></a>";
			}
			pb.append(a);
			if(i != end) {
				pb.append(" | ");
			}
		}
		
		if(!isLast) {
			fn = funcName + "(" + i + ")";
			a  = " | <a href='javascript:" + fn + "'>"+ next_image +"</a>";
			pb.append(a);
		}
		
		
	}
	
	public void makePB2() {
		pb = new StringBuffer();
		int lastPageNum;
		boolean isLast = false;
		boolean isFirst = false;
		
		totPage = (tot / paging_size) + (tot%paging_size==0?0:1);
		if(curPage <= paging_block_size) {
			isFirst = true;
		}
		
		if( (curPage-1) / paging_block_size == (totPage-1) / paging_block_size) {
			lastPageNum = totPage % paging_block_size;
			if(lastPageNum != paging_block_size) {
				isLast =true;
			}
			if(lastPageNum == 0) {
				lastPageNum = paging_block_size;
			}
		}
		else {
			lastPageNum = paging_block_size;
		}
		int start = curPage - (curPage%paging_block_size == 0 ? paging_block_size : curPage%paging_block_size) + 1;
		int end = curPage - (curPage%paging_block_size == 0 ? paging_block_size : curPage%paging_block_size) + lastPageNum;
		
		System.out.println("totPage : " + totPage);
		System.out.println("lastPageNum : " + lastPageNum);
		System.out.println("curPage : " + curPage);
		System.out.println("start : " + start);
		System.out.println("end : " + end);
		if(start > end) end = start;
		String fn = "";
		String a  = "";
		int i = 0;
		if(!isFirst) {
			fn = funcName + "(" + (start-1) + ")";
			a = "<a href='javascript:" + fn + "' style='text-decoration:none; border: 1px solid gray;' >" + prev_image + "</a>";
			pb.append(a);
		}
		for(i = start ; i <= end ; i++) {
			fn = funcName + "(" + i + ")";
			if(i == curPage) {
				a = "<font color='"+selected_color+"' style='text-decoration:none; border: 1px solid gray;' >&nbsp;&nbsp; " + i + " &nbsp;&nbsp;</font>";
			}
			else {
				a = "<a href='javascript:" + fn + "' style='text-decoration:none; border: 1px solid gray;' ><font color='"+normal_color+"'>&nbsp;&nbsp; " + i + " &nbsp;&nbsp;</font></a>";
			}
			pb.append(a);
			if(i != end) {
		//		pb.append(" | ");
			}
		}
		
		if(!isLast) {
			fn = funcName + "(" + i + ")";
			a  = "<a href='javascript:" + fn + "'>"+ next_image +"</a>";
			pb.append(a);
		}
		
		
	}
	
	public void makePB3() {
		pb = new StringBuffer();
		int lastPageNum;
		boolean isLast = false;
		boolean isFirst = false;
		
		totPage = (tot / paging_size) + (tot%paging_size==0?0:1);
		if(curPage <= paging_block_size) {
			isFirst = true;
		}
		
		if( (curPage-1) / paging_block_size == (totPage-1) / paging_block_size) {
			lastPageNum = totPage % paging_block_size;
			if(lastPageNum != paging_block_size) {
				isLast =true;
			}
			if(lastPageNum == 0) {
				lastPageNum = paging_block_size;
			}
		}
		else {
			lastPageNum = paging_block_size;
		}
		int start = curPage - (curPage%paging_block_size == 0 ? paging_block_size : curPage%paging_block_size) + 1;
		int end = curPage - (curPage%paging_block_size == 0 ? paging_block_size : curPage%paging_block_size) + lastPageNum;
		
		System.out.println("totPage : " + totPage);
		System.out.println("lastPageNum : " + lastPageNum);
		System.out.println("curPage : " + curPage);
		System.out.println("start : " + start);
		System.out.println("end : " + end);
		if(start > end) end = start;
		String fn = "";
		String a  = "";
		int i = 0;
		if(!isFirst) {
			fn = funcName + "(" + (start-1) + ")";
			a = "<a href='javascript:" + fn + "' style='text-decoration:none; border: 1px solid gray;' >" + prev_image + "</a>";
			pb.append(a);
		}
		for(i = start ; i <= end ; i++) {
			fn = funcName + "(" + i + ")";
			if(i == curPage) {
				a = "<font color='"+selected_color+"' style='text-decoration:none; border: 1px solid gray;' >&nbsp;&nbsp; " + i + " &nbsp;&nbsp;</font>";
			}
			else {
				a = "<a href='javascript:" + fn + "' style='text-decoration:none; border: 1px solid gray;' ><font color='"+normal_color+"'>&nbsp;&nbsp; " + i + " &nbsp;&nbsp;</font></a>";
			}
			pb.append(a);
			if(i != end) {
				//		pb.append(" | ");
			}
		}
		
		if(!isLast) {
			fn = funcName + "(" + i + ")";
			a  = "<a href='javascript:" + fn + "'>"+ next_image +"</a>";
			pb.append(a);
		}
		
		
	}
	
	public void makePB4() {
		pb = new StringBuffer();
		int lastPageNum = 0;
		boolean isLast = false;
		boolean isFirst = false;
		
		totPage = (tot / paging_size) + (tot%paging_size==0?0:1);
		if(curPage <= paging_block_size) {
			isFirst = true;
		}
		
		if( (curPage-1) / paging_block_size == (totPage-1) / paging_block_size) {
			lastPageNum = totPage % paging_block_size;
			if(lastPageNum != paging_block_size) {
				isLast =true;
			}
			if(lastPageNum == 0) {
				lastPageNum = 1;//paging_block_size;
			}
		}
		else {
			lastPageNum = paging_block_size;
		}
		int start = curPage - (curPage%paging_block_size == 0 ? paging_block_size : curPage%paging_block_size) + 1;
		int end = curPage - (curPage%paging_block_size == 0 ? paging_block_size : curPage%paging_block_size) + lastPageNum;
		
		System.out.println("totPage : " + totPage);
		System.out.println("lastPageNum : " + lastPageNum);
		System.out.println("curPage : " + curPage);
		System.out.println("start : " + start);
		System.out.println("end : " + end);
		System.out.println("isFirst : " + isFirst);
		System.out.println("isLast : " + isLast);
		if(start > end) end = start;
		String fn = "";
		String a  = "";
		int i = 0;
		if(!isFirst) {
			fn = funcName + "(" + (start-1) + ")";
			a = "<a href='javascript:" + fn + "' >" + prevX_image + "</a>";
			fn = funcName + "(" + (curPage-1) + ")";
			a+= "&nbsp;<a href='javascript:" + fn + "' >" + prev_image + "</a>&nbsp;";
			pb.append(a);
		}
		else {
			a = prevX_image;
			if(curPage == start) {
				a+= "&nbsp;" + prev_image;
			}
			else {
				fn = funcName + "(" + (curPage - 1) + ")";
				a+=  "&nbsp;<a href='javascript:" + fn + "'>" + prev_image + "</a>&nbsp;";
			}
			pb.append(a);
		}
		for(i = start ; i <= end ; i++) {
			fn = funcName + "(" + i + ")";
			if(i == curPage) {
				a = "<strong>"+i+"</strong>&nbsp;";
			}
			else {
				a = "<a href='javascript:" + fn + "'>" + i + "</a>&nbsp;";
			}
			pb.append(a);
			if(i != end) {
				//		pb.append(" | ");
			}
		}
		
		if(!isLast) {
			fn = funcName + "(" + (curPage + 1) + ")";
			a =  "<a href='javascript:'" + fn + "'>" + next_image + "</a>&nbsp;";
			fn = funcName + "(" + i + ")";
			a += "&nbsp;<a href='javascript:" + fn + "'>"+ nextX_image +"</a>";
			pb.append(a);
		}
		else {
			a  = nextX_image;
			if(curPage == end) {
				a = next_image + "&nbsp;" + a;
			}
			else {
				fn = funcName + "(" + (curPage + 1) + ")";
				a =  "&nbsp;<a href='javascript:" + fn + "'>" + next_image + "</a>&nbsp;" + a;
			}
			pb.append(a);
		}
		
		
	}

	
	public String getPagingBlock() {
		
		return pb.toString();
	}
}
